<template>
  <el-dialog v-model="visible" class="oldSelectDialog" title="查看数据" @close="handleClose">
    <div class="dialogCenter">
      <el-form ref="ruleForm" :data="pagination" :label-width="116">
        <el-row>
          <el-col :span="24">
            <el-row>
              <el-col :span="9">
                <el-form-item label="时间范围：" name="name">
                  <el-select v-model="timeValue" filterable placeholder="请选择" @change="handleTime">
                    <el-option v-for="(item, index) in timeData" :key="index" :label="item.value" :value="item.id"
                               title=""/>
                  </el-select>
                  <el-date-range-picker v-if="typeValue === 3" v-model="time" :placeholder="['开始日期', '结束日期']"
                                        @change="handleDate"/>
                </el-form-item>
              </el-col>
            </el-row>
          </el-col>
        </el-row>
      </el-form>

      <el-table ref="multipleTableRef" :data="dialogData" style="width: 100%">
        <el-table-column label="序号" type="index" width="120"/>
        <el-table-column label="原始值" prop="dataValue"/>
        <el-table-column label="时间" prop="alarmTime"/>
      </el-table>

      <Pagination
          v-show="pagination.total > 0"
          v-model:limit="pagination.pageSize"
          v-model:page="pagination.pageNum"
          :total="pagination.total"
          @pagination="getLists"
      />
    </div>
  </el-dialog>
</template>

<script setup>
import {ref, watch} from 'vue'
import {timeData} from '@/utils/commonData';
import {getNum} from '@/utils/validate';

const props = defineProps({
  dialogData: {
    type: Array,
    default: () => [],
  },
  dialogVisible: {
    type: Boolean,
    default: false,
  },
  pagination: {
    type: Object,
    default: () => ({}),
  },
  dataLoading: {
    type: Boolean,
    default: false,
  },
});
const emit = defineEmits([
  'handleCloseDialog',
  'handleSearch',
  'getList',
  'onPage'
]);
const regTable = ref();
const ruleForm = ref(null);
const visible = ref(false);
const time = ref([]);
const timeValue = ref(0);
const typeValue = ref(0);

watch(props, (val) => {
  visible.value = val.dialogVisible;
});

// 关闭弹层
const handleClose = () => {
  timeValue.value = 0;
  typeValue.value = 0;
  time.value = [];
  emit('handleCloseDialog');
};

const getLists = (val) => {
  emit('getList', val);
};

// 时间筛选、自定义除外
const handleTime = (val) => {
  typeValue.value = val;
  if (val < 3) {
    const data = timeData.filter((user) => user.id === val);
    emit('handleSearch', getNum(data[0].value));
    emit('onPage')
    time.value = [];
  }
};

// 日期筛选数据
const handleDate = (val) => {
  emit('handleSearch', val);
};
</script>
